package com.sdy.kzyy.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.kzyy.biz.dto.AppointDateDTO;
import com.sdy.kzyy.biz.model.KeyValueConfigurations;
import com.sdy.kzyy.biz.mapper.KeyValueConfigurationsMapper;
import com.sdy.kzyy.biz.service.KeyValueConfigurationsService;
import com.sdy.kzyy.biz.service.StoreInventoryService;
import com.sdy.kzyy.biz.utils.TimeChecker;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 常量配置 服务实现类
 * </p>
 *
 * @author zzq
 * @since 2020-03-12
 */
@Slf4j
@Service
public class KeyValueConfigurationsServiceImpl extends BaseServiceImpl<KeyValueConfigurations> implements KeyValueConfigurationsService {
    @Autowired
    private KeyValueConfigurationsMapper keyValueConfigurationsMapper;
    @Autowired
    private StoreInventoryService storeInventoryService;

    @Override
    public String getOrderInitiationInterval() {
        return list(new QueryWrapper<KeyValueConfigurations>().
                eq("kzyykey", "ORDER_INITIATION_INTERVAL")).get(0).getKzyyvalue();
    }

    @Override
    public String getAppointmentTime() {
        return list(new QueryWrapper<KeyValueConfigurations>().
                eq("kzyykey", "APPOINTMENT_TIME")).get(0).getKzyyvalue();
    }

    @Override
    public String getLimitationCount() {
        return list(new QueryWrapper<KeyValueConfigurations>().
                eq("kzyykey", "LIMITATION_COUNT")).get(0).getKzyyvalue();
    }

    @Override
    public String getDayFrequency() {
        return list(new QueryWrapper<KeyValueConfigurations>().
                eq("kzyykey", "DAY_FREQUENCY")).get(0).getKzyyvalue();
    }

    @Override
    public String getExtractTime() {
        return list(new QueryWrapper<KeyValueConfigurations>().
                eq("kzyykey", "EXTRACT_TIME")).get(0).getKzyyvalue();
    }

    @Override
    public String getOverdueDate() {
        return list(new QueryWrapper<KeyValueConfigurations>().
                eq("kzyykey", "OVERDUE_DATE")).get(0).getKzyyvalue();
    }

    @Override
    public String getQrcodeAddress() {
        return list(new QueryWrapper<KeyValueConfigurations>().
                eq("kzyykey", "QRCODE_ADDRESS")).get(0).getKzyyvalue();
    }

    @Override
    public String getAppointmentWeek() {
        return list(new QueryWrapper<KeyValueConfigurations>().
                eq("kzyykey", "APPOINTMENT_WEEK")).get(0).getKzyyvalue();
    }

    @Override
    public AppointDateDTO getAppointDateDTO() {
        String appointmentWeek = getAppointmentWeek();
        String appointmentTime = getAppointmentTime();
        String overdueDate = getOrderInitiationInterval();
        String limitationCount = getLimitationCount();
        AppointDateDTO appointDateDTO = new AppointDateDTO()
                .setAppointmentWeek(appointmentWeek)
                .setAppointmentTime(appointmentTime)
                .setOverdueDate(Integer.valueOf(overdueDate))
                .setLimitationCount(Integer.valueOf(limitationCount));
        return appointDateDTO;
    }

    @Override
    public Boolean updateAppointDate(AppointDateDTO appointDateDTO) throws BizException {
        // 校验
        Assert.isBlank(appointDateDTO.getAppointmentWeek(), "请选择(周一~周日)!");
        Assert.isBlank(appointDateDTO.getAppointmentTime(), "请填写时间!");
        Assert.isTrue(appointDateDTO.getOverdueDate() == null, "请填写天数!");
        Assert.isTrue(appointDateDTO.getLimitationCount() == null, "请填写个数!");

        Map<String, Object> objectObjectMap = new LinkedHashMap<>();
        objectObjectMap.put("APPOINTMENT_WEEK", appointDateDTO.getAppointmentWeek());
        objectObjectMap.put("APPOINTMENT_TIME", appointDateDTO.getAppointmentTime());
        objectObjectMap.put("ORDER_INITIATION_INTERVAL", appointDateDTO.getOverdueDate());
        objectObjectMap.put("LIMITATION_COUNT", appointDateDTO.getLimitationCount());

        List<String> key = new ArrayList<>();
        key.add("APPOINTMENT_WEEK");
        key.add("APPOINTMENT_TIME");
        key.add("ORDER_INITIATION_INTERVAL");
        key.add("LIMITATION_COUNT");

        // 判断是否在预约时间内
        Assert.isTrue((TimeChecker.check(getAppointmentTime(),getAppointmentWeek())), "请在当前可预约时间范围之外再进行维护操作!");
        for (int i = 0; i < objectObjectMap.size(); i++) {
            if (objectObjectMap.get(key.get(i)) != "" && objectObjectMap.get(key.get(i)) != null) {
                boolean update = update(Wrappers.<KeyValueConfigurations>lambdaUpdate()
                        .eq(KeyValueConfigurations::getKzyykey, key.get(i))
                        .set(KeyValueConfigurations::getKzyyvalue, objectObjectMap.get(key.get(i)))
                );
                Assert.isTrue(!update, "更新数据出错!");
            }
        }
        // 修改同步信息
        if(storeInventoryService.updateStoreOnceCount()) {
            log.info("可预约口罩数修改成功!");
        }else {
            log.info("可预约口罩数修改失败!");
        }
        return true;
    }

}
